<?php
class Country_PollController extends Core_Controller_Action_Standard {
    
    
    public function popupAction(){
        $this->view->status = true;       
        $country_id = $this->_getParam('country_id',0);
        $pollTable = Engine_Api::_()->getDbTable('polls', 'Country');
        $rows = $pollTable->fetchAll();
        foreach($rows as $row){
            $this->view->body .= $this->view->partial('_displayQuestionpoll.tpl', 'country', array('poll' => $row, 'options' => $row->getOptions(), 'noInfo' => $this->_getParam('noInfo'),'country_id'=>$country_id));
        }
        
    }
    public function voteAction() {
        $this->view->status = false;       
        $country_id = $this->_getParam('country_id',0);
        
        if ($this->getRequest()->isPost()) {
            $viewer = Engine_Api::_()->user()->getViewer();
            if ($viewer->getIdentity()) {
                $poll = $this->_getParam('poll');
                $opt  = (int) $this->_getParam('option');
                $show_all = $this->_getParam('show_all', 0); 
                $pollTable = Engine_Api::_()->getDbTable('polls', 'Country');
                if ($poll && $opt) {
                    $question = $pollTable->find($poll)->current();
                    if ($question) {
                        $option = Engine_Api::_()->getDbTable('polloptions', 'Country')->fetchRow("polloption_id = '$opt'");
                        if ($option) {
                            $vote = Engine_Api::_()->getDbTable('Pollvotes', 'Country')->fetchRow("user_id = '{$viewer->getIdentity()}' AND polloption_id = {$option->getIdentity()} and country_id = {$country_id} ");
                            if (!$vote) {
                                $now = date('Y-m-d H:i:s');
                                Engine_Api::_()->getDbTable('Pollvotes', 'Country')->insert(array(
                                    'user_id' => $viewer->getIdentity(),
                                    'polloption_id' => $option->getIdentity(),
                                    'poll_id' => $question->poll_id,
                                    'country_id' => $country_id
                                    
                                ));
                                $type = 'vote';                                
                            } else {
                                $vote->delete();
                                $type = NULL;    
                            }
                            $option->save();
                            $question->save();
                            //$question->broadcast($type, array('viewer' => $viewer, 'answer' => $option->getTitle()));
                            $this->view->status = true;
                            $rows = $pollTable->fetchAll();
                            foreach($rows as $row){
                                $this->view->body  .= $this->view->partial('_displayQuestionpoll.tpl', 'country', array('poll' => $row, 'options' => $row->getOptions(), 'noInfo' => $this->_getParam('noInfo'),'country_id'=>$country_id));
                            }
                             
                        }
                    }
                }
            }
        }
    }
    
   
}
